package com.example.myhomework02;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class MainActivity extends AppCompatActivity{


    private Button btnMC;
    private Button btnMR;
    private Button btnMAdd;
    private Button btnMJian;
    private Button btnMS;
    private Button btnM;
    private Button btnMod;
    private Button btnSqr;
    private Button btnPingfang;
    private Button btn1chuX;
    private Button btnCE;
    private Button btnC;
    private Button btnBack;
    private Button btnChu;
    private Button btn7;
    private Button btn8;
    private Button btn9;
    private Button btnCheng;
    private Button btn4;
    private Button btn5;
    private Button btn6;
    private Button btnJian;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btnAdd;
    private Button btnAddAnd;
    private Button btn0;
    private Button btnDian;
    private Button btnEquils;
    private TextView display;

    String str = "";
    List<Double> numlist = new LinkedList<>();
    List<String> strList = new LinkedList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnMod = (Button) findViewById(R.id.btn_mod);
        btnSqr = (Button) findViewById(R.id.btn_sqr);
        btnCE = (Button) findViewById(R.id.btn_CE);
        btnC = (Button) findViewById(R.id.btn_C);
        btnBack = (Button) findViewById(R.id.btn_back);
        btnChu = (Button) findViewById(R.id.btn_chu);
        btn7 = (Button) findViewById(R.id.btn_7);
        btn8 = (Button) findViewById(R.id.btn_8);
        btn9 = (Button) findViewById(R.id.btn_9);
        btnCheng = (Button) findViewById(R.id.btn_cheng);
        btn4 = (Button) findViewById(R.id.btn_4);
        btn5 = (Button) findViewById(R.id.btn_5);
        btn6 = (Button) findViewById(R.id.btn_6);
        btnJian = (Button) findViewById(R.id.btn_jian);
        btn1 = (Button) findViewById(R.id.btn_1);
        btn2 = (Button) findViewById(R.id.btn_2);
        btn3 = (Button) findViewById(R.id.btn_3);
        btnAdd = (Button) findViewById(R.id.btn_add);
        btnAddAnd = (Button) findViewById(R.id.btn_addAnd_);
        btn0 = (Button) findViewById(R.id.btn_0);
        btnDian = (Button) findViewById(R.id.btn_dian);
        btnEquils = (Button) findViewById(R.id.btn_equils);
        display = (TextView)findViewById(R.id.tv_display) ;

        /**
         * 面板数字显示
         */
        btn0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                str = str + "0";
                display.setText(str);
            }
        });
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                str = str + "1";
                display.setText(str);
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                str = str + "2";
                display.setText(str);
            }
        });
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                str = str + "3";
                display.setText(str);
            }
        });
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                str = str + "4";
                display.setText(str);
            }
        });
        btn5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                str = str + "5";
                display.setText(str);
            }
        });
        btn6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                str = str + "6";
                display.setText(str);
            }
        });
        btn7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                str = str + "7";
                display.setText(str);
            }
        });
        btn8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                str = str + "8";
                display.setText(str);
            }
        });
        btn9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                str = str + "9";
                display.setText(str);
            }
        });

        /**
         * 除法计算
         * 1、将str ——> double
         * 2、清空str
         */
        btnChu.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                double d = Double.parseDouble(str);
                numlist.add(d);
                str = "";
                strList.add("/");
            }
        });
        /**
         * 乘法计算
         * 1、将str ——> double
         * 2、清空str
         */
        btnCheng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                double d = Double.parseDouble(str);
                numlist.add(d);
                str = "";
                strList.add("X");
            }
        });
        /**
         * 减法计算
         * 1、负数
         *   1.1 添加负数符号
         * 2、减法
         *   2.1 将str ——> double
         *   2.2 清空str
         *   2.3 记录运算符
         */
        btnJian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                double d = 0;
                //1、如果为负数，2、单纯减法
                if(str.length() == 0){
                    str = str + "-";
                    display.setText(str);
                }else{
                    d = Double.parseDouble(str);
                    numlist.add(d);
                    str = "";
                    strList.add("-");
                }
                Log.i("TAG",String.valueOf(d));
            }
        });
        /**
         * 加法运算
         */
        btnAdd.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                double d = Double.parseDouble(str);
                numlist.add(d);
                str = "";
                strList.add("+");
            }
        });
        btnEquils.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                //1、将最后一个数存入集合中
                double d = Double.parseDouble(str);
                numlist.add(d);
                /*2、首先从左往右进行乘除运算
                    2.1将对应的两边数进行运算，前一个数进行存储新值，后一个数销毁或者将两数销毁在对应位置插入数值
                    2.2将运算符销毁  */
                for (int j = 0; j < strList.size(); j++) {
                    double n1 =0 , n2 = 0 , result = 0;
                    switch (strList.get(j)){
                        case "X":
                            n1 = numlist.get(j);
                            n2 = numlist.get(j+1);
                            result = n1 * n2;
                            numlist.remove(j);
                            numlist.remove(j);
                            numlist.add(j,result);
                            strList.remove(j);
                            break;
                        case "/":
                            n1 = numlist.get(j);
                            n2 = numlist.get(j+1);
                            result = n1 / n2;
                            numlist.remove(j);
                            numlist.remove(j);
                            numlist.add(j,result);
                            strList.remove(j);
                            break;
                    }
                }
//                3、将减去的数变为负数存储
                for (int j = 0; j < strList.size(); j++) {
                    if(strList.get(j) == "-"){
                        double m = numlist.get(j+1)*(-1);
                        numlist.remove(j+1);
                        numlist.add(j+1,m);
                    }
                }
//                4、遍历进行加法计算
                double out = 0;
                for (int j = 0; j < numlist.size(); j++) {
                    out = out + numlist.get(j);
                }
                if(out % 1 == 0){
                    display.setText(String.valueOf(((int) out)));
                }else {
                    display.setText(String.valueOf(out));
                }
            }
        });
        /**
         * 重置操作
         */
        btnC.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                numlist.clear();
                str = "";
                strList.clear();
                display.setText("0");
            }
        });
    }

}